home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE23 / EVENTLST / HOOKLBL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-05-12  |  2.1 KB  |  85 lines

  1. { *****************************************************
  2.                          THookedLabel Component
  3.  
  4.   A trivial example of a component which hooks an event from 
  5.   a TEventList.
  6.  
  7.                     Paul Warren
  8.            HomeGrown Software Development
  9.          (c) 1997 Langley British Columbia.
  10.                   (604) 856-6523
  11.            e-mail:  hg_soft@uniserve.com
  12.      Home page: http://users.uniserve.com/~hg_soft
  13.   ***************************************************** }
  14.  
  15. unit Hooklbl;
  16.  
  17. interface
  18.  
  19. uses
  20.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  21.   Forms, Dialogs, StdCtrls, HookdBtn;
  22.  
  23. type
  24.   TMessageKind = (mkDate, mkTime, mkUpbeat);
  25.  
  26.   THookLabel = class(TLabel)
  27.   private
  28.     { private declarations }
  29.     FMessageKind: TMessageKind;
  30.     FSource: THookedButton;
  31.     procedure SetSource(Value: THookedButton);
  32.   protected
  33.     { protected declarations }
  34.   public
  35.     { public declarations }
  36.     constructor Create(AOwner: TComponent); override;
  37.     procedure UpdateLabel(Sender: TObject);
  38.   published
  39.     { published declarations }
  40.     property MessageKind: TMessageKind read FMessageKind write FMessageKind;
  41.     property Source: THookedButton read FSource write SetSource;
  42.   end;
  43.  
  44. procedure Register;
  45.  
  46. implementation
  47.  
  48. constructor THookLabel.Create(AOwner: TComponent);
  49. var
  50.   i: integer;
  51. begin
  52.   inherited Create(AOwner);
  53.   for i := 0 to TForm(AOwner).ComponentCount-1 do
  54.   begin
  55.     if TForm(AOwner).Components[i] is THookedButton then
  56.     begin
  57.       FSource := THookedButton(TForm(AOwner).Components[i]);
  58.       Break;
  59.     end;
  60.   end;
  61. end;
  62.  
  63. procedure THookLabel.UpdateLabel(Sender: TObject);
  64. begin
  65.   case MessageKind of
  66.     mkDate: Caption := DateToStr(Date);
  67.     mkTime: Caption := TimeToStr(Time);
  68.     mkUpbeat: Caption := 'What a wonderful day';
  69.   end;
  70. end;
  71.  
  72. procedure THookLabel.SetSource(Value: THookedButton);
  73. begin
  74.   FSource := Value;
  75.   { if successful hook HookEvent }
  76.   if (FSource <> nil) then
  77.      FSource.HookEvent := UpdateLabel;
  78. end;
  79.  
  80. { register component on Samples page }
  81. procedure Register;
  82. begin
  83.   RegisterComponents('Samples', [THookLabel]);
  84. end;
  85.  
  86. end.